﻿using B.S.DataBase.MyProject.ErrorCode;
using B.S.Traceability.Domain;
using B.S.Traceability.Infrastructure.Interface;
using B.S.Traceability.Read.API.Applicantion.Command.GoodsCommand;
using B.S.Traceability.Read.API.Dtos;
using MediatR;

namespace B.S.Traceability.Read.API.Applicantion.CommandHandler.GoodsCommandHandler
{
    public class QueryGoodsCommandHandler : IRequestHandler<QueryGoodsCommand, ApiPaging<GoodsDTO>>
    {
        private readonly IBaseRepository<SU_GoodName> goodNameRep;
        private readonly IBaseRepository<SU_GoodsInfo> goodRep;
        public QueryGoodsCommandHandler(IBaseRepository<SU_GoodName> goodNameRep, IBaseRepository<SU_GoodsInfo> goodRep)
        {
            this.goodNameRep = goodNameRep;
            this.goodRep = goodRep;
        }

        public async Task<ApiPaging<GoodsDTO>> Handle(QueryGoodsCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<GoodsDTO> result = new ApiPaging<GoodsDTO>();
            var list = from a in await goodNameRep.GetAll()
                       join b in await goodRep.GetAll() on a.GoodNameId equals b.GoodNameId
                       where b.IsDel == false
                       select new GoodsDTO
                       {
                           GoodsId=b.GoodsId,
                           GoodNameId= a.GoodNameId,
                           ProductionCode = b.ProductionCode,
                           SellByDay = b.SellByDay,
                           ProNumber = b.ProNumber,
                           ProductionEnt = b.ProductionEnt,
                           Improve = b.Improve,
                           GoodsPhone = b.GoodsPhone,
                           CreateName = b.CreateName,
                           CreateTime = b.CreateTime,
                           UpdateName = b.UpdateName,
                           UpdateTime = b.UpdateTime,
                           IsDel = b.IsDel,
                           GoodName = a.GoodName,
                       };
            if (request.GoodNameId!=null)
            {
                list = list.Where(x => x.GoodNameId == request.GoodNameId);
            }
            if (!string.IsNullOrEmpty(request.ProductionCode))
            {
                list = list.Where(x => x.ProductionCode == request.ProductionCode);
            }
            if (request.SellByDay != null)
            {
                list = list.Where(x => x.SellByDay == request.SellByDay);
            }
            if (!string.IsNullOrEmpty(request.ProNumber))
            {
                list = list.Where(x => x.ProNumber == request.ProNumber);
            }
            if (!string.IsNullOrEmpty(request.ProductionEnt))
            {
                list = list.Where(x => x.ProductionEnt == request.ProductionEnt);
            }
            if (!string.IsNullOrEmpty(request.CreateName))
            {
                list = list.Where(x => x.CreateName == request.CreateName);
            }
            if (!string.IsNullOrEmpty(request.UpdateName))
            {
                list = list.Where(x => x.UpdateName == request.UpdateName);
            }
            result.TotalCount = list.Count();
            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
            list=list.OrderByDescending(x => x.GoodsId).Skip(request.PageSize*(request.PageIndex-1)).Take(request.PageSize);
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();
            result.list = list.ToList();
            return await Task.FromResult(result);
        }
    }
}
